/**
 * 云豆充值服务
 * 提供云豆充值相关功能
 */

const request = require('../libs/request.js')

/**
 * 获取用户云豆余额
 * @returns {Promise<Object>}
 */
const getBalance = async () => {
    try {
        const response = await request.authGet('/v1/score-charge/balance')

        if (response.code === 1) {
            return {
                success: true,
                data: response.data,
                message: response.msg
            }
        } else {
            throw new Error(response.msg || '获取云豆余额失败')
        }
    } catch (error) {
        console.error('获取云豆余额失败:', error)
        return {
            success: false,
            message: error.message || '获取云豆余额失败'
        }
    }
}

/**
 * 创建云豆充值订单
 * @param {Object} chargeData 充值数据 {amount, custom_amount}
 * @returns {Promise<Object>}
 */
const createCharge = async (chargeData) => {
    try {
        const response = await request.authPost('/v1/score-charge/create', chargeData)

        if (response.code === 1) {
            return {
                success: true,
                data: response.data,
                message: response.msg
            }
        } else {
            throw new Error(response.msg || '创建充值订单失败')
        }
    } catch (error) {
        console.error('创建充值订单失败:', error)
        return {
            success: false,
            message: error.message || '创建充值订单失败'
        }
    }
}

/**
 * 查询充值订单状态
 * @param {string} chargeId 充值订单ID
 * @returns {Promise<Object>}
 */
const queryChargeStatus = async (chargeId) => {
    try {
        const response = await request.authGet('/v1/score-charge/status', {
            data: {
                charge_id: chargeId
            }
        })

        if (response.code === 1) {
            return {
                success: true,
                data: response.data,
                message: response.msg
            }
        } else {
            throw new Error(response.msg || '查询充值状态失败')
        }
    } catch (error) {
        console.error('查询充值状态失败:', error)
        return {
            success: false,
            message: error.message || '查询充值状态失败'
        }
    }
}

/**
 * 获取充值记录列表
 * @param {Object} params 查询参数 {page, limit}
 * @returns {Promise<Object>}
 */
const getChargeRecords = async (params = {}) => {
    try {
        const response = await request.authGet('/v1/score-charge/records', {
            data: {
                page: params.page || 1,
                limit: params.limit || 10
            }
        })

        if (response.code === 1) {
            return {
                success: true,
                data: response.data,
                message: response.msg
            }
        } else {
            throw new Error(response.msg || '获取充值记录失败')
        }
    } catch (error) {
        console.error('获取充值记录失败:', error)
        return {
            success: false,
            message: error.message || '获取充值记录失败'
        }
    }
}

module.exports = {
    getBalance,
    createCharge,
    queryChargeStatus,
    getChargeRecords
} 